#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <jsoncpp/json/json.h>

using namespace std;
int main()
{
    
    const char* name = "小明";
    int age=18;
    float score[] = {77.4,23.6,32.6};
    Json::Value root;
    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);

    Json::StreamWriterBuilder swb;
    unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    std::stringstream ss;
    sw->write(root,&ss);
    cout<<ss.str()<<endl;

    //string str = R"({"姓名":"小黑","年龄":19,"成绩":[59.0,23,50]})";
    string str = ss.str();
    Json::CharReaderBuilder crb;
    unique_ptr<Json::CharReader> cr(crb.newCharReader());
    string err;
    bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
    if (ret == false)
    {
        cout<<"parse error:"<<err<<endl;
        return -1;
    }
    cout<<root["姓名"].asString()<<endl;
    cout<<root["年龄"].asInt()<<endl;
    int sz = root["成绩"].size();
    for(int i=0;i<sz;++i)
    {
        cout<<root["成绩"][i]<<" ";
    }
    cout<<endl;
    return 0;
}